<?php
/*
	[Destoon B2B System] Copyright (c) 2008-2015 www.destoon.com
	This is NOT a freeware, use is subject to license.txt
*/
defined('DT_ADMIN') or exit('Access Denied');
require('phpQuery/phpQuery.php');
$menus = array (
    array('重建缓存', '?file='.$file.'&action=cache'),
    array('模板管理', '?file=template'),
    array('风格管理', '?file=skin'),
    array('标签向导', '?file='.$file),
);
switch($action) {
	case 'cache':
		cache_clear('htm', 'dir', 'tag');
		dmsg('更新成功', '?file='.$file);
	break;
	case 'spider':
	   switch($siteid)
	   {
		   case 'memberAdd':
				
				$module='member';
				$moduleid=4;
				$num = 0;
				$hostdir="/mnt/photo" ;
				 require DT_ROOT.'/module/'.$module.'/common.inc.php';
				 $MOD = $moduleid == 2 ? $EXT : cache_read('module-'.$moduleid.'.php');
				 include DT_ROOT.'/lang/'.DT_LANG.'/'.$module.'.inc.php';
				 $FD = $MFD = cache_read('fields-member.php');
				 $CFD = cache_read('fields-company.php');
				 if($MFD || $CFD) require DT_ROOT.'/include/fields.func.php';
				 $GROUP = cache_read('group.php');
				 require DT_ROOT.'/module/member/member.class.php';
				 require DT_ROOT.'/randName.php';
				$cjname = 'jpg';
				 $filesnames = scandir($hostdir);
			
				  foreach ($filesnames as $name) {
					
					  if($name!=".." && $name!=".")
					  {
						  	$data['truename'] =  getRandName();
						
							$pingying = new Pinyin;
							$data['username'] =  $pingying->getpy($data['truename']);
							$data['password']=$data['username'];
							$data['cpassword']=$data['username'];
							$data['areaid']=2; 
							$data['regid']=5; 
							$data['mobile'] = '179'.rand(10000000,99999999); 
							$data['email'] = $data['username'].'@163.com'; 
							$data['gender']=rand(1,2); 
							$data['passport']=trim($data['username']); ///账户名 
							$data['edittime']=1;
							$data['groupid']=5; ///分组
						  $md5 = md5($data['username']);
						 
					      $fn='file/avatar/'.substr($md5, 0, 2).'/'.substr($md5, 2, 2);
						
						  mkdir ($fn,0777,true);
						  
						  $name1 = '/'.substr($md5, 0, 2).'/'.substr($md5, 2, 2).'/_'.$data['username'];
						  $result = copy($hostdir.'/'.$name,'file/avatar'.$name1.'.jpg');
							if($result){
								$do = new member;
								if($do->get_one($data['username'])){
									continue;
								}else{
									 echo "会员入库开始：".$data['truename'].'<br>';	
									  if($do->add($data)){
										$num++;
									  }else{
										   echo "会员信息入库失败".$data['truename'].'<br>';		
									  }
										
								}
							}
					 }
					 echo "\n";
				 }
				
			
				echo '成功添加会员'.$num.'个';
				
				break;
		   case 'tianyan':
		      $startime=microtime();
			  $spidercount=0;
		      $data=array();
			   //error_reporting(E_ALL);
			 $module='member';
			 $moduleid=4;
			 $num = 0;
			 $sum = 0;
			 $sizeArray = array('0'=>'1-49人','1'=>'50-99人','2'=>'100-499人','3'=>'500-999人','4'=>'1000-3000人','5'=>'3000-5000人','6'=>'5000-10000人','7'=>'10000人以上');
			 $modeArray = array('制造商','其他机构','贸易商','服务商');
			 require DT_ROOT.'/module/'.$module.'/common.inc.php';
			 $MOD = $moduleid == 4 ? $EXT : cache_read('module-'.$moduleid.'.php');
			 include DT_ROOT.'/lang/'.DT_LANG.'/'.$module.'.inc.php';
			 $FD = $MFD = cache_read('fields-member.php');
			 $CFD = cache_read('fields-company.php');
			 if($MFD || $CFD) require DT_ROOT.'/include/fields.func.php';
			 $GROUP = cache_read('group.php');
			 require DT_ROOT.'/module/member/member.class.php';
		
			  //echo $url;
			  //error_reporting(E_ALL);
			 for($x=1;$x<100;$x++){ //分页请求
				$p = 'p'.$x;
				$keyword = $_GET['keyword'];
			   $html = 'https://www.gkzhan.com';
			   $url = $html.'/Company/a_t0/list_'.$p.'_tid0_ch0_an_cp_k'.$keyword.'.html';
			   $comment = get_url_info($url);
			   $str ='#(<td width="471"\s+height="48"\s+align="left">\s+<p class="title">\s+(<em\s+class="level[0-9]"><\/em>)?\s+<a\s+href="(\/st([0-9]+)|(\/Company\/Detail\/([0-9]+)\.html+))"\s+target="\_blank")\s+title="(.+?)"#is';
			   preg_match_all($str ,$comment ,$tmp);
			   $detailList = $tmp[3];
	
			   if(empty($detailList)){
				   break;
			   }
			   foreach( $detailList as $key=>$val){
				  if(strstr($val,"html")){ //未认证公司
					    $comment1 = get_url_info($html.$val);
				  }else{//认证公司
					 $comment1 = get_url_info($html.$val.'/Intro.html');
					 // $comment1 = get_url_info('https://www.gkzhan.com/st22888/Intro.html');
				  }
				    $str = '/<body.+?>(.+?)<\/body>/is';
					 preg_match($str , $comment1 , $attr);
				
					 phpQuery::newDocumentHTML($attr[0]); 
					 
			
				  $companyInfo = pq('.intro_box:contains("公司名称") ')->html(); //st161171/Intro.html模式
				  
				  $companyInfo1 = pq('.inside-box:contains("公司名称") ')->html();//st42402/Intro.html
				  $companyInfo2 = pq('.leftcompany')->html();///st165818/intro.html模式
				  $companyInfo3 = pq('.contact')->html();//st169217/intro.html模式//Company/Detail/169516.html
				  $companyInfo4 = pq('.right_box_bg:last')->html(); //st22888/Intro.html模式
				
		
				$arr = array();
				  if( $companyInfo){//st161171/Intro.html模式
					  $str1 = '/<span>(.+?)<\/span>(.+?)<(s|\/d)/is';
					  preg_match_all($str1 , $companyInfo , $attr);
					  if($attr[1])
					  {
						  $arr = array();
						  foreach($attr[1] as $k=>$v){
							  if(str_replace("<br>","",$attr[2][$k]) && !strstr($attr[2][$k],"<a")){
								    $arr[$v] = trim(str_replace("<br>","",$attr[2][$k]));
							  }
						  }
						  $arr['intro'] = trim(strip_tags(pq('.intro_box:eq(0) > .text')->html()));
						  $arr['business'] = trim(strip_tags(pq('.intro_box:eq(1) > .text')->html()));
						
					  }
				  }else if($companyInfo1){ //st42402/Intro.html
					foreach( pq('.companycontact > tr') as $item ) {
							$k1 =trim(strip_tags(pq($item)->find("th:eq(0)")->html()));
							$arr[$k1]  = trim(strip_tags(pq($item)->find("td:eq(0)")->html()));
						}
						  $arr['intro'] =trim(strip_tags(pq('.inside-box:eq(0) > .inside-boxmain')->html()));
						  $arr['intro'] = trim(strip_tags(pq('.inside-box:eq(0) > .inside-boxmain')->html()));
						  $arr['thumb'] =pq(".companyname > img")->attr('src');
				
				  }else if($companyInfo2){ //st165818/intro.html模式
					foreach( pq('.leftcompany > dl') as $item ) {
							$k1 =trim(strip_tags(pq($item)->find("dt:eq(0)")->html()));
							$arr[$k1]  = trim(strip_tags(pq($item)->find("dd:eq(0)")->html()));
						}
						 $arr['intro'] = trim(strip_tags(pq('.introbox:eq(0)')->html()));
						 $arr['business'] = trim(strip_tags(pq('.certification > p')->html()));
						 $arr['thumb'] = pq('#zhantai_logo')->attr('src');
						
				  }else if($companyInfo3){ 
					if(pq('.contactor > dl')){//st169217/intro.html模式
						foreach( pq('.contactor > dl') as $item ) {
							$k1 =trim(strip_tags(pq($item)->find("dt:eq(0)")->html()));
							$arr[$k1]  = trim(strip_tags(pq($item)->find("dd:eq(0)")->html()));
						}
						 $arr['intro'] = trim(strip_tags(pq('.aboutUsText:eq(0) ')->html()));
						 $arr['business'] = trim(strip_tags(pq('.aboutUsText:eq(1)')->html()));
						  $arr['thumb'] = pq('#zhantai_logo')->attr('src');
					}
					if(pq('.contact > li')){//Company/Detail/169516.html
						foreach( pq('.contact > li') as $item ) {
							$k1 =trim(strip_tags(pq($item)->html()));
							$info =  explode(':', $k1);
							$k1 = trim($info[0]).'：';
							$arr[$k1]  = trim($info[1]);
						}
						$arr['intro'] = trim(strip_tags(pq('.qiugou_box:eq(1)')->html()));
						 $arr['business'] = trim(strip_tags(pq('.qiugou_box:eq(0)')->html()));
					}
					
				  }else if($companyInfo4){//st22888/Intro.html模式
					  phpQuery::newDocumentHTML($companyInfo4); 
						foreach( pq('tr') as $item ) {
							$k1 = pq($item)->find("td:eq(0)")->html();
							
							$arr[$k1]  = pq($item)->find("td:eq(1)")->html();
						
						}
						phpQuery::newDocumentHTML($attr[0]); 
						$arr['intro'] = trim(strip_tags(pq('.nr_box:eq(0)')->html()));
						$arr['business'] = trim(strip_tags(pq('.nr_box:eq(1)')->html()));
						$arr['thumb'] = pq('#zhantai_logo')->attr('src');
						
				  }else{
						
						$str4 = '/<div class\=\"gsjs\_right\_02\">\s+(.+?)\s+<\/div>/is';
						preg_match_all($str4 , $comment1 , $attr);
						
						if($attr[1][0]){
							phpQuery::newDocumentHTML($attr[1][0]); 
							foreach( pq('tr') as $item ) {
								$k1 = pq($item)->find("td:eq(0)")->html();
								
								$arr[$k1]  = pq($item)->find("td:eq(1)")->html();
							
							}
						}
					}
				   $sum ++;
				  if($arr){
					$data = array();
					  $data['outname']=trim($tmp[7][$key]); 
					  $data['truename'] =$arr['联 系 人：']?$arr['联 系 人：']:$arr['法定代表人：']; 
					  $data['truename'] =$data['truename']?$data['truename']:$arr['联  系  人：']; 
					  $data['truename'] =$data['truename']?$data['truename']:$arr['联 系 人 ：']; 
					  $data['truename'] =$data['truename']?$data['truename']:$arr['联系人：']; 
					  $data['mobile'] =$arr['手机号码：']; 
					  $data['mobile'] =$data['mobile']?$data['mobile']:$arr['手机：']; 
					  $data['mobile'] =$data['mobile']?$data['mobile']:$arr['手　　机：']; 
					  $mobile = explode(',', $data['mobile']);
					  $data['mobile'] = $mobile[0];
					  $data['content']=!empty($arr['intro'])?$arr['intro']:'';	
					  $data['business']=!empty($arr['business'])?$arr['business']:'';	
					  $data['thumb']=!empty($arr['thumb'])?$arr['thumb']:'';	
					 
					  $data['address'] =$arr['详细地址：']?$arr['详细地址：']:$arr['地址：'];
				
					  $data['address'] = $data['address']?$data['address']:$arr['地址：'];
						$data['username']='c'.$data['mobile']; 
						$data['password']=substr($data['mobile'],-6); 
						$data['cpassword']=$data['password']; 
						$data['gender']= 1; 
						$data['areaid']= 2; 
						$data['catid']= ','.rand(5,183).','; 
						$data['size']= $sizeArray[rand(0,7)]; 
						$data['mode'][]= $modeArray[rand(0,3)]; 
						$data['regunit']= '人民币'; 
						$data['capital']= rand(50,1000); 
						$data['regid']= 1; 
						$data['vcompany']= 1; 
						$data['validated']= 1; 
						$data['regyear']= (int)rand(2003,2016); 
						$data['company']=  $data['outname']; 
						$data['type']= '企业单位'; 
						$data['telephone']=  $data['mobile']; 
						$data['email'] = trim($data['mobile'])."@189.cn";
						$data['inviter']=  ''; 
						$data['passport']=trim($data['mobile']); ///账户名 
						$data['groupid']=6; ///分组
						$data['edittime']=1;
				    	$do = new member;
						if($do->get_one($data['username'])){
							continue;
						}else{
							 //echo "公司信息入库开始：".$data['company'].'<br>';		
				
							  if($do->add($data)){
								$num++;
							  }else{
								   // echo "公司信息入库失败".$data['company'].'<br>';		
							  }
								
						}
				  }
				  
			   }
			 } 
			//phpQuery::selectDocument($text);
			echo "公司信息入库成功".$num.'条'.'总条数'. $sum .'条';		
		   break;
		   default:
		     echo "未选择采集对象";
		   
	   }
	  
	break;
	default:
		$table_select = $all_select = '';
		$out = array('ad', 'ad_place', 'admin', 'alert', 'area', 'ask' ,'category', 'favorite', 'finance_cash', 'finance_charge', 'finance_record', 'finance_trade', 'friend', 'group', 'guestbook', 'keylink', 'log', 'mail', 'mail_list', 'message', 'module', 'session', 'style', 'type', 'vip');
		$query = $db->query("SHOW TABLE STATUS FROM `".$CFG['db_name']."`");
		while($r = $db->fetch_row($query)) {
			$table = $r[0];
			$alltable = preg_match("/^".$DT_PRE."/i", $table) ? substr($table, strlen($DT_PRE)) : $table.'&prefix=';
			$all_select .= '<option value="'.$alltable.'">'.$table.'</option>';
			if(substr($table, -5) == '_data' || strpos($table, '_data_') !== false) continue;
			if(preg_match("/^".$DT_PRE."/i", $table)) {
				$table = substr($table, strlen($DT_PRE));
				if(in_array($table, $out)) continue;
				$s = $db->get_one("SHOW TABLE STATUS FROM `".$CFG['db_name']."` LIKE '".$r[0]."'");
				$table_select .= '<option value="'.$table.'">'.($s['Comment'] ? $s['Comment'] : $table).'</option>';         
			}
		}
		$mid or $mid = '';
		include tpl('spider');
	break;
}


function get_url_info($url){
	
	// 1. 初始化
 $ch = curl_init();
 // 2. 设置选项，包括URL
 curl_setopt($ch,CURLOPT_URL,$url);
 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
 curl_setopt($ch,CURLOPT_HEADER,0);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//绕过ssl验证
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

 // 3. 执行并获取HTML文档内容
 $output = curl_exec($ch);
 if($output === FALSE ){
 echo "CURL Error:".curl_error($ch);
 }
 // 4. 释放curl句柄
 curl_close($ch);
    return $output;
}

function getRandName(){
	$arrXing = array('赵','钱','孙','李','周','吴','郑','王','冯','陈','褚','卫','蒋','沈','韩','杨','朱','秦','尤','许','何','吕','施','张','孔','曹','严','华','金','魏','陶','姜','戚','谢','邹','喻','柏','水','窦','章','云','苏','潘','葛','奚','范','彭','郎','鲁','韦','昌','马','苗','凤','花','方','任','袁','柳','鲍','史','唐','费','薛','雷','贺','倪','汤','滕','殷','罗','毕','郝','安','常','傅','卞','齐','元','顾','孟','平','黄','穆','萧','尹','姚','邵','湛','汪','祁','毛','狄','米','伏','成','戴','谈','宋','茅','庞','熊','纪','舒','屈','项','祝','董','梁','杜','阮','蓝','闵','季','贾','路','娄','江','童','颜','郭','梅','盛','林','钟','徐','邱','骆','高','夏','蔡','田','樊','胡','凌','霍','虞','万','支','柯','管','卢','莫','柯','房','裘','缪','解','应','宗','丁','宣','邓','单','杭','洪','包','诸','左','石','崔','吉','龚','程','嵇','邢','裴','陆','荣','翁','荀','于','惠','甄','曲','封','储','仲','伊','宁','仇','甘','武','符','刘','景','詹','龙','叶','幸','司','黎','溥','印','怀','蒲','邰','从','索','赖','卓','屠','池','乔','胥','闻','莘','党','翟','谭','贡','劳','逄','姬','申','扶','堵','冉','宰','雍','桑','寿','通','燕','浦','尚','农','温','别','庄','晏','柴','瞿','阎','连','习','容','向','古','易','廖','庾','终','步','都','耿','满','弘','匡','国','文','寇','广','禄','阙','东','欧','利','师','巩','聂','关','荆','司马','上官','欧阳','夏侯','诸葛','闻人','东方','赫连','皇甫','尉迟','公羊','澹台','公冶','宗政','濮阳','淳于','单于','太叔','申屠','公孙','仲孙','轩辕','令狐','徐离','宇文','长孙','慕容','司徒','司空');
	$numbXing = count($arrXing);
	$arrMing = array('伟','刚','勇','毅','俊','峰','强','军','平','保','东','文','辉','力','明','永','健','世','广','志','义','兴','良','海','山','仁','波','宁','贵','福','生','龙','元','全','国','胜','学','祥','才','发','武','新','利','清','飞','彬','富','顺','信','子','杰','涛','昌','成','康','星','光','天','达','安','岩','中','茂','进','林','有','坚','和','彪','博','诚','先','敬','震','振','壮','会','思','群','豪','心','邦','承','乐','绍','功','松','善','厚','庆','磊','民','友','裕','河','哲','江','超','浩','亮','政','谦','亨','奇','固','之','轮','翰','朗','伯','宏','言','若','鸣','朋','斌','梁','栋','维','启','克','伦','翔','旭','鹏','泽','晨','辰','士','以','建','家','致','树','炎','德','行','时','泰','盛','雄','琛','钧','冠','策','腾','楠','榕','风','航','弘','秀','娟','英','华','慧','巧','美','娜','静','淑','惠','珠','翠','雅','芝','玉','萍','红','娥','玲','芬','芳','燕','彩','春','菊','兰','凤','洁','梅','琳','素','云','莲','真','环','雪','荣','爱','妹','霞','香','月','莺','媛','艳','瑞','凡','佳','嘉','琼','勤','珍','贞','莉','桂','娣','叶','璧','璐','娅','琦','晶','妍','茜','秋','珊','莎','锦','黛','青','倩','婷','姣','婉','娴','瑾','颖','露','瑶','怡','婵','雁','蓓','纨','仪','荷','丹','蓉','眉','君','琴','蕊','薇','菁','梦','岚','苑','婕','馨','瑗','琰','韵','融','园','艺','咏','卿','聪','澜','纯','毓','悦','昭','冰','爽','琬','茗','羽','希','欣','飘','育','滢','馥','筠','柔','竹','霭','凝','晓','欢','霄','枫','芸','菲','寒','伊','亚','宜','可','姬','舒','影','荔','枝','丽','阳','妮','宝','贝','初','程','梵','罡','恒','鸿','桦','骅','剑','娇','纪','宽','苛','灵','玛','媚','琪','晴','容','睿','烁','堂','唯','威','韦','雯','苇','萱','阅','彦','宇','雨','洋','忠','宗','曼','紫','逸','贤','蝶','菡','绿','蓝','儿','翠','烟');
	$numbMing = count($arrMing);
	$name = $arrXing[mt_rand(0,$numbXing)].$arrMing[mt_rand(0,$numbMing)].$arrMing[mt_rand(0,$numbMing)];
	return $name;
}

	

 



?>